<?php
class Tag extends MY_Controller {
	function __construct(){
		parent::__construct();
	}
	function admin_index(){
		$this->_assign("adminIndex", TRUE);
		$this->load->model('tag_model','tagModel');
		$tag = $this->tagModel->get(array("order"=>"id ASC"));
		$this->_assign('tags',$tag);

		$this->_fetch('admin_index');
	}
	function admin_add($id=""){
		$this->_assign("adminIndex", TRUE);
		if(!empty($id)){
			$this->load->model("tag_model","tagModel");
	 	 	$tag = $this->tagModel->get($id);
	 	 	$this->_assign('tag',$tag);
	 	 	$this->_fetch('admin_add');
		}else{
			$_post = $this->input->post();
			if(empty($_post["submit"])){
				$this->_fetch('admin_add');
			}else{
				//Delete cache
				$this->cache->delete_folder("category");
				$this->cache->delete("menu_widget_html");
				$this->load->model('tag_model','tagModel');
				$where['where']['name'] = $_post["name"];
				$addname['id'] = $_post['id'];
				$addname['name'] = $_post['name'];
				//$this->tagModel->add($addname);
				$get = $this->tagModel->get($where);


				if(!empty($get) OR $get == false){
					$_post["url"] = Util::url_title($_post["name"]);
					$result = $this->tagModel->add($_post);
					if($result){
						redirect(base_url("admin/tag"));
					}else{
						redirect(base_url("admin/tag/add"));
					}
				}else{
					redirect(base_url("admin/tag/add"));
				}
			}
		}
	}
	 function admin_delete($id = ""){
	 	if(!empty($id)){
	 		$this->db->delete('ci_tag',array('tag_id'=>$id));
	 	}

	 	//Delete cache
		$this->cache->delete_folder("category");
		$this->cache->delete("menu_widget_html");
		
	 	redirect('admin/tag');
	 }




}